<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;

//商品管理控制器
class GoodsController extends CommonController{
	private $model;
	public function __construct(){
		//为了防止父级覆盖，再次调用一下
		parent::__construct();	
		//实例化方法	
		$this->model=new \Common\Model\Goods;	
		
	}
	
	//添加商品
	public function add(){
		
		//判断是否提交
		if(IS_POST){
//			sp($_POST);die;
			//调用模型里面的方法
			if($this->model->addData()) {
//				sp($_POST);exit;
				View::success('添加成功',U('index'));
			}
			//验证失败
			View::error($this->model->getError());
		}

	    //1.处理所属分类
	    $cateModel=new \Common\Model\Cate;
		//把所有的分类呈树状结构显示出来
		$cateData=Data::tree($cateModel->get(),'cname');
		//分配变量
		View::with('cateData',$cateData);
		//2.处理品牌
		$brandModel=new \Common\Model\Brand;
		//获得所有的品牌
		$brandData=$brandModel->get();
		//分配变量
		View::with('brandData',$brandData);
		//载入页面
		View::make();
	}
	
	public function add_attr(){
		
		$tid = Q('post.tid',0,'intval');
		//"select * from typeattribute where type_tid = {$tid}"
		$typeattrModel = new \Common\Model\TypeAttribute;
		
		$data = $typeattrModel->where("type_tid={$tid}")->get();
		
		
		foreach($data as $k=>$v){
			$data[$k]['avalue'] = explode(",", $v['avalue']);
		}
		$this->ajax($data);
	}
	
	
	
	//上传处理
	public function upload(){
			
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	//显示商品
	public function index(){
	   $data=$this->model->limit(Page::limit())->get();
		View::with('data',$data);
		View::make();
	}
	//删除商品
	public function del(){
		//获取get 的参数
		$gid=Q('get.gid',0,'intval');
	    //删除
	    $this->model->where("gid={$gid}")->delete();
		//删除提示
		View::success('删除成功',U('index'));
	}
	//编辑商品
	public function edit(){
		//修改
		if(IS_POST){
			//执行修改  成功提示
			if($this->model->editData()) View::success('修改成功',U('index'));
			//错误提示
			View::error($this->model->getError());
		}
		//处理所属分类
		$cateModel=new \Common\Model\Cate;
		
		//获取数组结构
		$cateData=Data::tree($cateModel->get(),'cname','cid','pid');
	

		//分配数据
		View::with('cateData',$cateData);
		//2.处理品牌
		$brandModel=new \Common\Model\Brand;
		$brandData=$brandModel->get();
		//分配变量
		View::with('brandData',$brandData);
		//获取get传递过来的gid
		
		//3.获取旧数据
		$gid=Q('get.gid',0,'intval');
		//分配数据
		$oldData=$this->model->where("gid={$gid}")->find();
		//获取售后以及商品详情
		$detailModel=new \Common\Model\GoodsData;
		$oldDetail=$detailModel->where("goods_gid={$gid}")->find();
		//分配数据
		View::with('oldDetail',$oldDetail);
		
		//商品表和商品详细表关联
		$bigData = Db::table("goods")->join("detail","gid","=","goods_gid")->where("gid={$gid}")->find();
		
		$attrData = Db::table("goods_attr")->join("typeattribute","aid","=","typeAttribute_aid")->where("goods_gid={$gid}")->get();
		foreach($attrData as $k=>$v){
			$attrData[$k]['avalue'] = explode(",", $v['avalue']);
		}
		View::with("bigData",$bigData);
		View::with('oldData',$oldData);
		View::with('attrData',$attrData);
		View::make();
	}

	
}



 ?>